package com.josework.springhelloworld.utils.forkjoin;

import java.util.stream.Stream;

public class WordCounter {
    private final int counter;
    private final boolean lastSpace;

    public WordCounter(int counter, boolean lastSpace) {
        this.counter = counter;
        this.lastSpace = lastSpace;
    }

    /**
     * 和迭代算法一样，accumulate方法一个个遍历Character
     * @param c
     * @return
     */
    public WordCounter accumulate(Character c){
        if (Character.isWhitespace(c)){
            return lastSpace ?
                    this :
                    new WordCounter(counter,true);
        }else {
            //上一个字符是空格，而当前遍历的字符不是空格时候，将单词计数器加一
            return lastSpace ?
                    new WordCounter(counter+1,false):
                    this;
        }
    }

    /**
     * 合并两个WordCounter，把其计数器累加起来
     * @param wordCounter
     * @return
     */
    public WordCounter combine(WordCounter wordCounter){
        //仅需计数器的总和，无需关心lastSpace
        return  new WordCounter(counter+wordCounter.counter
                ,wordCounter.lastSpace);
    }

    public int getCounter(){
        return counter;
    }

    public static int countWords(Stream<Character> stream){
        WordCounter wordCounter = stream.reduce(
                new WordCounter(0,true),
                WordCounter::accumulate,
                WordCounter::combine
        );
        return wordCounter.getCounter();
    }




}

